package com.akbar.security;

import com.akbar.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SecurityUser implements UserDetails {
    private SysUser sysUser;
    private List<GrantedAuthority> authorities;

    // 用户权限集合
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    // 密码
    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    // 用户名
    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    // 用户是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }

    // 用户是否未被锁定：
    // 在用户连续5次登陆失败，就锁定
    @Override
    public boolean isAccountNonLocked() {
        return sysUser.getStatus() == 1;
    }

    // 用户凭证（密码）是否未过期。
    // 若项目需要定期强制用户修改密码可自行扩展实现。
    @Override
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }


    @Override
    public boolean isEnabled() {
        return sysUser.getStatus() == 1;
    }
}
